package StarterRedigo

import (
	"fmt"
	"github.com/go-spring/spring-boot"
	"github.com/go-spring/starter-redis"
	"github.com/gomodule/redigo/redis"
	"time"
)

func init() {
	SpringBoot.RegisterNameBeanFn("redigo-client", func(config StarterRedis.Config) (*redis.Pool, error) {
		p := redis.Pool{
			MaxIdle:     256,
			MaxActive:   0,
			IdleTimeout: time.Duration(180),
			Dial: func() (redis.Conn, error) {
				return redis.Dial(
					"tcp",
					fmt.Sprintf("%s:%d", config.Host, config.Port),
					redis.DialReadTimeout(time.Duration(1000)*time.Millisecond),
					redis.DialWriteTimeout(time.Duration(1000)*time.Millisecond),
					redis.DialConnectTimeout(time.Duration(1000)*time.Millisecond),
					redis.DialDatabase(config.Database),
					redis.DialPassword(config.Password),
				)
			},
		}
		return &p, nil
	}).ConditionOnMissingBean((*redis.Pool)(nil))
}
